package cn.taketoday.live.http;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.data.LoginData;
import cn.taketoday.live.data.Qrcode;
import cn.taketoday.live.data.UserBarrageMsg;
import cn.taketoday.live.data.user.User;
import cn.taketoday.live.data.user.UserManager;
import cn.taketoday.live.data.user.UserMedal;
import cn.taketoday.live.handler.BarrageHandler;
import cn.taketoday.live.tools.CurrencyTools;
import cn.taketoday.live.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Headers;
import okhttp3.Response;

/**
 * @author TODAY
 */
@Slf4j
public class BilibiliHttpRequests {

  /**
   * 初始化 获取用户信息+判断是否登陆状态
   */
  public static void getUser() {
    String data = null;
    JSONObject jsonObject = null;
    Map<String, String> headers = null;
    headers = new HashMap<>(2);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    try {
      data = HttpUtils.getString("https://account.bilibili.com/home/USERInfo", headers, null);
    }
    catch (Exception e) {

      log.error("", e);
      data = null;
    }
    if (data == null)
      return;
    jsonObject = JSONObject.parseObject(data);
    short code = jsonObject.getShort("code");
    if (code == 0) {
      log.debug("已经登录: {}", jsonObject);
    }
    else if (code == -101) {
      log.debug("未登录: {}", jsonObject);
    }
    else {
      log.error("未知错误,原因未知 {}", jsonObject);
    }
  }

  /**
   * 获取登陆二维码
   */
  public static Qrcode getQrcode() {
    String data = null;
    JSONObject jsonObject = null;
    Qrcode qrcode = null;
    Map<String, String> headers = null;
    headers = new HashMap<>(2);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    try {
      data = HttpUtils
              .getString("https://passport.bilibili.com/qrcode/getLoginUrl", headers, null);
    }
    catch (Exception e) {

      log.error("", e);
      data = null;
    }
    if (data == null)
      return qrcode;
    jsonObject = JSONObject.parseObject(data);
    short code = jsonObject.getShort("code");
    if (code == 0) {
      qrcode = JSONObject.parseObject(jsonObject.getString("data"), Qrcode.class);
    }
    else {
      log.error("获取二维码失败,未知错误,原因未知 {}", jsonObject);
    }
    return qrcode;
  }

  /**
   * 判断扫码状态 扫码确定后 获取用户cookie
   */
  public static String getCookie(LoginData logindata) {
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      return "";
    }
    String data = null;
    Response response = null;
    Map<String, String> headers = null;
    Map<String, String> params = null;
    headers = new HashMap<>(3);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://passport.bilibili.com/login");
    params = new HashMap<>(3);
    params.put("oauthKey", logindata.getOauthKey());
    params.put("gourl", logindata.getGourl());
    try {
      response = HttpUtils.post("https://passport.bilibili.com/qrcode/getLoginInfo",
                                headers, params);
      data = response.body().string();
      if (JSONObject.parseObject(data).getBoolean("status")) {
        Headers headers2 = response.headers();
        List<String> cookies = headers2.values("Set-Cookie");
        Set<String> cookieSet = new HashSet<>();
        for (String string : cookies) {
          cookieSet.add(string.substring(0, string.indexOf(";")));
        }
        StringBuilder stringBuilder = new StringBuilder(100);
        Iterator<String> iterable = cookieSet.iterator();
        while (iterable.hasNext()) {
          stringBuilder.append(iterable.next());
          if (iterable.hasNext()) {
            stringBuilder.append(";");
          }
        }
        GlobalData.USERCOOKIE = stringBuilder.toString();
        if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
          if (GlobalData.ROOM_ID != null) {
            getUserBarrageMessage();
          }
          log.debug("扫码登录成功");
        }
      }
    }
    catch (Exception e1) {

      log.error("扫码登录失败抛出异常", e1);
    }

    return data;
  }

  /**
   * 获取用户信息 需要cookie 初始化
   */
  public static void getUserInfo() {
    String data = null;
    JSONObject jsonObject = null;
    Map<String, String> headers = null;
    headers = new HashMap<>(3);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    try {
      data = HttpUtils.getString("https://api.live.bilibili.com/User/getUserInfo", headers, null);
    }
    catch (Exception e) {
      log.error("", e);
      data = null;
    }
    if (data == null)
      return;
    jsonObject = JSONObject.parseObject(data);
    if (jsonObject.getString("code").equals("REPONSE_OK")) {
      GlobalData.USER = new User();
      GlobalData.USER = JSONObject.parseObject(jsonObject.getString("data"), User.class);
      log.debug("已经登录，获取信息成功");
    }
    else if (jsonObject.getShort("code") == -500) {
      log.debug("未登录，请登录: {}", jsonObject.toString());
      GlobalData.USERCOOKIE = null;
      GlobalData.USER = null;
    }
    else {
      log.error("未知错误,原因未知 {}", jsonObject.toString());
      GlobalData.USERCOOKIE = null;
      GlobalData.USER = null;
    }
  }

  /**
   * 获取用户所在房间是否是房管
   */
  public static void userIsManager() {
    String data = null;
    JSONObject jsonObject = null;
    Map<String, String> headers = null;
    headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://live.bilibili.com/" + CurrencyTools.parseRoomId());
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    try {
      data = HttpUtils
              .getString("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByUser?room_id="
                                 + CurrencyTools.parseRoomId(), headers, null);
    }
    catch (Exception e) {

      log.error("httpGetUserIsManager", e);
      data = null;
    }
    if (data == null)
      return;
    jsonObject = JSONObject.parseObject(data);
    short code = jsonObject.getShort("code");
    if (code == 0) {
      log.debug("获取本房间自身管理信息成功");
      if (GlobalData.ROOM_ID != null && GlobalData.ROOM_ID > 0) {
        Boolean manager = jsonObject.getJSONObject("data").getJSONObject("badge").getBoolean("is_room_admin");
        GlobalData.USERMANAGER = new UserManager();
        GlobalData.USERMANAGER.setIs_manager(manager != null ? manager : false);
        GlobalData.USERMANAGER.setRoomid(GlobalData.ROOM_ID);
        GlobalData.USERMANAGER.setShort_roomid(CurrencyTools.parseRoomId());
      }
    }
    else if (code == -101) {
      log.debug("未登录，请登录: {}", jsonObject.toString());
    }
    else if (code == -400) {
      log.debug("房间号不存在或者未输入房间号: {}", jsonObject.toString());
    }
    else {
      log.error("未知错误,原因未知 {}", jsonObject.toString());
    }
  }

  /**
   * 获取用户在目标房间所能发送弹幕的最大长度
   */
  public static void getUserBarrageMessage() {
    String data = null;
    JSONObject jsonObject = null;
    Map<String, String> headers = null;
    headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://live.bilibili.com/" + CurrencyTools.parseRoomId());
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    try {
      data = HttpUtils
              .getString("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByUser?room_id="
                                 + CurrencyTools.parseRoomId(), headers, null);
    }
    catch (Exception e) {
      log.error("httpGetUserBarrageMsg", e);
      data = null;
    }
    if (data == null)
      return;
    jsonObject = JSONObject.parseObject(data);
    short code = jsonObject.getShort("code");
    if (code == 0) {
      log.debug("获取本房间可发送弹幕长度+是否是管理员 成功");
      final JSONObject data1 = jsonObject.getJSONObject("data");
      GlobalData.USER_BARRAGE_MESSAGE = data1.getJSONObject("property").toJavaObject(UserBarrageMsg.class);
      Boolean manager = jsonObject.getJSONObject("data").getJSONObject("badge").getBoolean("is_room_admin");
      GlobalData.USERMANAGER = new UserManager();
      GlobalData.USERMANAGER.setIs_manager(manager != null ? manager : false);
      GlobalData.USERMANAGER.setRoomid(GlobalData.ROOM_ID);
      GlobalData.USERMANAGER.setShort_roomid(CurrencyTools.parseRoomId());
    }
    else if (code == -101) {
      log.debug("未登录，请登录: {}", jsonObject);
    }
    else if (code == -400) {
      log.debug("房间号不存在或者未输入房间号: {}", jsonObject);
    }
    else {
      log.error("未知错误,原因未知 {}", jsonObject);
    }
  }

  /**
   * 获取用户在目标房间所能发送弹幕的最大长度
   */
  public static UserBarrageMsg getUserBarrageMessage(Long roomId) {
    String data = null;
    JSONObject jsonObject = null;
    Map<String, String> headers = null;
    headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://live.bilibili.com/" + roomId);
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    try {
      data = HttpUtils
              .getString("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByUser?room_id="
                                 + roomId, headers, null);
    }
    catch (Exception e) {
      log.error("httpGetUserBarrageMsg", e);
      data = null;
    }
    if (data == null)
      return null;
    jsonObject = JSONObject.parseObject(data);
    short code = jsonObject.getShort("code");
    if (code == 0) {
      log.debug("获取本房间可发送弹幕长度成功");
      final JSONObject data1 = jsonObject.getJSONObject("data");
      return data1.getJSONObject("property").toJavaObject(UserBarrageMsg.class);
    }
    else if (code == -101) {
      log.debug("未登录，请登录: {}", jsonObject);
    }
    else if (code == -400) {
      log.debug("房间号不存在或者未输入房间号: {}", jsonObject);
    }
    else {
      log.error("未知错误,原因未知 {}", jsonObject);
    }
    return null;
  }

  /**
   * 发送弹幕
   *
   * @param barrage
   *         弹幕信息
   */
  public static int sendBarrage(String barrage) {
    if (GlobalData.USER_BARRAGE_MESSAGE == null || GlobalData.COOKIE == null)
      return -1;

    Map<String, String> headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://live.bilibili.com/" + CurrencyTools.parseRoomId());
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }

    Map<String, String> params = new HashMap<>(10);
    params.put("color", GlobalData.USER_BARRAGE_MESSAGE.getDanmu().getColor().toString());
    params.put("fontsize", "25");
    params.put("mode", GlobalData.USER_BARRAGE_MESSAGE.getDanmu().getMode().toString());
    params.put("msg", barrage);
    params.put("rnd", String.valueOf(System.currentTimeMillis()).substring(0, 10));
    params.put("roomid", GlobalData.ROOM_ID.toString());
    params.put("bubble", GlobalData.USER_BARRAGE_MESSAGE.getBubble().toString());
    params.put("csrf_token", GlobalData.COOKIE.getBili_jct());
    params.put("csrf", GlobalData.COOKIE.getBili_jct());

    String data = getData(params, headers, "https://api.live.bilibili.com/msg/send");
    if (data == null)
      return -1;
    int code = -1;
    JSONObject jsonObject = JSONObject.parseObject(data);
    if (jsonObject != null) {
      code = jsonObject.getIntValue("code");
      final String message = jsonObject.getString("message");
      if (code != 0) {
        log.error("发送弹幕'{}'失败,原因: {}", barrage, message);
      }
      else if (StringUtils.isNotEmpty(message)) {
//        if ("msg repeat".equals(message) || "msg in 1s".equals(message)) {
          log.warn("重新发送弹幕'{}'失败,原因: {}", barrage, message);
          BarrageHandler.send(barrage); // 什么键盘
//        }
//        else {
//          log.error("发送弹幕'{}'失败,原因: {}", barrage, jsonObject);
//        }
      }
    }
    return code;
  }

  private static String getData(Map<String, String> params, Map<String, String> headers, String url) {
    try {
      return HttpUtils.postString(url, headers, params);
    }
    catch (Exception e) {
      log.error("getData", e);
    }
    return null;
  }

  /**
   * 发送弹幕
   *
   * @param msg
   *         弹幕信息
   */
  public static Short sendBarrage(String msg, Long roomId) {
    JSONObject jsonObject = null;
    short code = -1;
    Map<String, String> headers = null;
    Map<String, String> params = null;
    UserBarrageMsg userBarrageMsg = getUserBarrageMessage(roomId);
    if (userBarrageMsg == null || GlobalData.COOKIE == null)
      return code;
    headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://live.bilibili.com/" + roomId);
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    params = new HashMap<>(10);
    params.put("color", userBarrageMsg.getDanmu().getColor().toString());
    params.put("fontsize", "25");
    params.put("mode", userBarrageMsg.getDanmu().getMode().toString());
    params.put("msg", msg);
    params.put("rnd", String.valueOf(System.currentTimeMillis()).substring(0, 10));
    params.put("roomid", roomId.toString());
    params.put("bubble", userBarrageMsg.getBubble().toString());
    params.put("csrf_token", GlobalData.COOKIE.getBili_jct());
    params.put("csrf", GlobalData.COOKIE.getBili_jct());
    String data = getData(params, headers, "https://api.live.bilibili.com/msg/send");
    if (data == null)
      return code;
    jsonObject = JSONObject.parseObject(data);
//		System.out.println(jsonObject.toJSONString().toString());
    if (jsonObject != null) {
      code = jsonObject.getShort("code");
      if (code == 0) {
        if (StringUtils.isEmpty(jsonObject.getString("message").trim())) {
//				LOGGER.debug("发送弹幕成功");
        }
        else if (jsonObject.getString("message").equals("msg in 1s")
                || jsonObject.getString("message").equals("msg repeat")) {
          log.debug("发送弹幕失败，尝试重新发送 {}", jsonObject.getString("message"));
        }
        else {
          log.debug(jsonObject.toString());
          log.error("发送弹幕失败,原因: {}", jsonObject.getString("message"));
          code = -402;
        }
      }
      else if (code == -111) {
        log.error("发送弹幕失败,原因: {}", jsonObject.getString("message"));
      }
      else if (code == -500) {
        log.error("发送弹幕失败,原因: {}", jsonObject.getString("message"));
      }
      else if (code == 11000) {
        log.error("发送弹幕失败,原因:弹幕含有关键字或者弹幕颜色不存在: {}", jsonObject.getString("message"));
      }
      else {
        log.error("发送弹幕失败,未知错误,原因未知 {}", jsonObject.toString());
      }
    }
    else {
      return code;
    }
    return code;
  }

  /**
   * 发送私聊
   *
   * @param recId
   *         接受人uid
   * @param msg
   *         信息
   */
  public static int sendMessage(long recId, String msg) {
    JSONObject jsonObject = null;
    int code = -1;
    Map<String, String> headers = null;
    Map<String, String> params = null;
    if (GlobalData.COOKIE == null)
      return code;
    if (GlobalData.USER.getUid() == recId)
      return code;
    headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://message.bilibili.com/");
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    params = new HashMap<>(16);
    params.put("msg[sender_uid]", GlobalData.USER.getUid().toString());
    params.put("msg[receiver_id]", String.valueOf(recId));
    params.put("msg[receiver_type]", "1");
    params.put("msg[msg_type]", "1");
    params.put("msg[msg_status]", "0");
    params.put("msg[content]", "{\"content\":\"" + msg + "\"}");
    params.put("msg[timestamp]", String.valueOf(System.currentTimeMillis()).substring(0, 10));
    params.put("msg[new_face_version]", "1");
    params.put("msg[dev_id]", UUID.randomUUID().toString());
    params.put("from_firework", "0");
    params.put("build", "0");
    params.put("mobi_app", "web");
    params.put("csrf_token", GlobalData.COOKIE.getBili_jct());
    params.put("csrf", GlobalData.COOKIE.getBili_jct());
    String data = getData(params, headers, "https://api.vc.bilibili.com/web_im/v1/web_im/send_msg");
    if (data == null)
      return code;
    jsonObject = JSONObject.parseObject(data);
    code = jsonObject.getIntValue("code");
    if (code == 0) {
      // 发送私聊成功
    }
    else {
      log.error("发送私聊失败,未知错误,原因未知 {}", jsonObject.toString());
    }
    return code;
  }

  /**
   * 禁言
   *
   * @param uid
   *         被禁言人uid
   * @param hour
   *         禁言时间 单位小时
   */
  public static Short addBlock(long uid, short hour) {
    short code = -1;
    if (hour < 1) {
      hour = 1;
    }
    if (hour > 720) {
      hour = 720;
    }
    if (GlobalData.COOKIE == null)
      return code;
    Map<String, String> headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://live.bilibili.com/" + CurrencyTools.parseRoomId());
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    Map<String, String> params = new HashMap<>(7);
    params.put("roomid", GlobalData.ROOM_ID.toString());
    params.put("block_uid", String.valueOf(uid));
    params.put("hour", String.valueOf(hour));
    params.put("csrf_token", GlobalData.COOKIE.getBili_jct());
    params.put("csrf", GlobalData.COOKIE.getBili_jct());
    params.put("visit_id", "");
    String data = getData(params, headers, "https://api.live.bilibili.com/banned_service/v2/Silent/add_block_user");
    if (data == null)
      return code;
    JSONObject jsonObject = JSONObject.parseObject(data);
    code = jsonObject.getShort("code");
    if (code == 0) {
      // 禁言成功
//			System.out.println(jsonObject.getString("data"));
    }
    else {
      log.error("禁言失败,原因 {}", jsonObject.getString("msg"));
    }
    return code;
  }

  public static String getUserFaces(long uid) {
    String data = null;
    JSONObject jsonObject = null;
    String faceUrl = null;
    Map<String, String> headers = null;
    headers = new HashMap<>(3);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://space.bilibili.com/" + uid);
    try {
      data = HttpUtils.getString("https://api.bilibili.com/x/space/acc/info?mid=" + uid, headers, null);
    }
    catch (Exception e) {
      log.error("", e);
      data = null;
    }
    if (data == null)
      return null;
    jsonObject = JSONObject.parseObject(data);
    short code = jsonObject.getShort("code");
    if (code == 0) {
      faceUrl = ((JSONObject) jsonObject.get("data")).getString("face");
    }
    else {
      log.error("获取头像错误,未知错误,原因未知 {}", jsonObject);
    }
    return faceUrl;
  }

  /**
   * 退出 删除cookie
   */
  public static void quit() {
    Map<String, String> headers = null;
    headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://www.bilibili.com/");
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    try {
      HttpUtils.get("https://passport.bilibili.com/login?act=exit", headers, null);
    }
    catch (Exception e) {
      log.error("", e);
    }
  }

  /**
   * 签到
   */
  public static void signIn() {
    String data = null;
    JSONObject jsonObject = null;
    Map<String, String> headers = null;
    headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://link.bilibili.com/p/center/index");
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    try {
      data = HttpUtils.getString("https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/DoSign", headers, null);
    }
    catch (Exception e) {
      log.error("", e);
      data = null;
    }
    if (data == null)
      return;
    jsonObject = JSONObject.parseObject(data);
    int code = jsonObject.getShort("code");
    if (code == 0) {
      log.debug(((JSONObject) jsonObject.get("data")).getString("specialText"));
    }
    else if (code == 1011040) {
      log.debug(jsonObject.getString("message"));
    }
    else {
      log.error("签到失败，原因： {}", jsonObject);
    }
  }

  public static List<UserMedal> getMedalList(int page) {
    String data = null;
    JSONObject jsonObject = null;
    JSONArray jsonArray = null;
    short code = -1;
    Map<String, String> headers = null;
    Map<String, String> params = null;
    headers = new HashMap<>(3);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    params = new HashMap<>(3);
    params.put("page", String.valueOf(page));
    params.put("pageSize", "25");
    try {
      data = HttpUtils.getString(
              "http://api.live.bilibili.com/fans_medal/v5/live_fans_medal/iApiMedal", headers, params);
    }
    catch (Exception e) {
      log.error("", e);
      data = null;
    }
    finally {
      headers.clear();
      params.clear();
    }
    if (data == null)
      return null;
    jsonObject = JSONObject.parseObject(data);
    code = jsonObject.getShort("code");
    if (code == 0) {
      jsonArray = jsonObject.getJSONObject("data").getJSONArray("fansMedalList");
      if (jsonArray != null) {
        return jsonArray.toJavaList(UserMedal.class);
      }
    }
    else {
      log.error("获取勋章失败，原因： {}", jsonObject);
    }
    return null;
  }

  //

  /**
   * 解除禁言
   */
  public static Short deleteBlock(long bid) {
    short code = -1;
    Map<String, String> headers = null;
    Map<String, String> params = null;
    if (GlobalData.COOKIE == null)
      return code;
    headers = new HashMap<>(4);
    headers.put("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
    headers.put("referer", "https://live.bilibili.com/" + CurrencyTools.parseRoomId());
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      headers.put("cookie", GlobalData.USERCOOKIE);
    }
    params = new HashMap<>(7);
    params.put("id", String.valueOf(bid));
    params.put("roomid", GlobalData.ROOM_ID.toString());
    params.put("csrf_token", GlobalData.COOKIE.getBili_jct());
    params.put("csrf", GlobalData.COOKIE.getBili_jct());
    params.put("visit_id", "");
    String data = getData(params, headers, "https://api.live.bilibili.com/banned_service/v1/Silent/del_room_block_user");
    if (data == null)
      return code;
    JSONObject jsonObject = JSONObject.parseObject(data);
    code = jsonObject.getShort("code");
    if (code == 0) {
      // 解禁成功
//			System.out.println(jsonObject.getString("data"));
    }
    else {
      log.error("解除禁言失败,原因 {}", jsonObject.getString("msg"));
    }
    return code;
  }
}
